# "Introduction to Zonas Básicas de Salud in the Comunidad de Madrid"
> "Now being used to apply Covid19 restrictions"

- toc:true- branch: master- badges: true- comments: true
- author: Alison Davey
- categories: [fastpages, jupyter]

#collapse-hide
!pip install geopandas
!apt-get install poppler-utils 
!pip install pdf2image

Requirement already satisfied: geopandas in /usr/local/lib/python3.6/dist-packages (0.8.1)
Requirement already satisfied: shapely in /usr/local/lib/python3.6/dist-packages (from geopandas) (1.7.1)
Requirement already satisfied: pyproj>=2.2.0 in /usr/local/lib/python3.6/dist-packages (from geopandas) (2.6.1.post1)
Requirement already satisfied: fiona in /usr/local/lib/python3.6/dist-packages (from geopandas) (1.8.17)
Requirement already satisfied: pandas>=0.23.0 in /usr/local/lib/python3.6/dist-packages (from geopandas) (1.0.5)
Requirement already satisfied: click<8,>=4.0 in /usr/local/lib/python3.6/dist-packages (from fiona->geopandas) (7.1.2)
Requirement already satisfied: cligj>=0.5 in /usr/local/lib/python3.6/dist-packages (from fiona->geopandas) (0.5.0)
Requirement already satisfied: click-plugins>=1.0 in /usr/local/lib/python3.6/dist-packages (from fiona->geopandas) (1.1.1)
Requirement already satisfied: munch in /usr/local/lib/python3.6/dist-packages (from fiona->geopandas) (2.5.0)
Requirement already satisfied: six>=1.7 in /usr/local/lib/python3.6/dist-packages (from fiona->geopandas) (1.15.0)
Requirement already satisfied: attrs>=17 in /usr/local/lib/python3.6/dist-packages (from fiona->geopandas) (20.2.0)
Requirement already satisfied: numpy>=1.13.3 in /usr/local/lib/python3.6/dist-packages (from pandas>=0.23.0->geopandas) (1.18.5)
Requirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.6/dist-packages (from pandas>=0.23.0->geopandas) (2018.9)
Requirement already satisfied: python-dateutil>=2.6.1 in /usr/local/lib/python3.6/dist-packages (from pandas>=0.23.0->geopandas) (2.8.1)
Reading package lists... Done
Building dependency tree       
Reading state information... Done
poppler-utils is already the newest version (0.62.0-2ubuntu2.10).
0 upgraded, 0 newly installed, 0 to remove and 11 not upgraded.
Requirement already satisfied: pdf2image in /usr/local/lib/python3.6/dist-packages (1.14.0)
Requirement already satisfied: pillow in /usr/local/lib/python3.6/dist-packages (from pdf2image) (7.0.0)

#collapse-hide
import geopandas
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable

from pdf2image import convert_from_path, convert_from_bytes
from pdf2image.exceptions import (
    PDFInfoNotInstalledError,
    PDFPageCountError,
    PDFSyntaxError
)

from PIL import Image

df=geopandas.read_file('./maps/zonas_basicas_salud.shp')
df.head()
codigo_geo pob_pad19 zona_basic geometry
0 001 30748 Abrantes POLYGON ((439068.758 4470731.661, 439076.433 4...
1 002 19432 Acacias POLYGON ((439924.930 4472798.281, 439928.742 4...
2 003 29168 Adelfas POLYGON ((443455.718 4472836.724, 443412.985 4...
3 004 21274 Alameda POLYGON ((440659.754 4473778.401, 440665.227 4...
4 005 29001 Alameda de Osuna POLYGON ((452408.138 4484644.700, 452415.138 4...
df.columns
Index(['codigo_geo', 'pob_pad19', 'zona_basic', 'geometry'], dtype='object')
df.info()
<class 'geopandas.geodataframe.GeoDataFrame'>
RangeIndex: 286 entries, 0 to 285
Data columns (total 4 columns):
 #   Column      Non-Null Count  Dtype   
---  ------      --------------  -----   
 0   codigo_geo  286 non-null    object  
 1   pob_pad19   286 non-null    int64   
 2   zona_basic  286 non-null    object  
 3   geometry    286 non-null    geometry
dtypes: geometry(1), int64(1), object(2)
memory usage: 9.1+ KB
plt.hist(df.pob_pad19)
plt.title('Distribution of population by areas')
plt.ylabel('Number of areas')
plt.xlabel('Population');
df.describe()
pob_pad19
count 286.000000
mean 23298.580420
std 9935.956169
min 2636.000000
25% 16702.250000
50% 21829.000000
75% 28164.250000
max 63789.000000
df[df.pob_pad19==df.pob_pad19.min()]
codigo_geo pob_pad19 zona_basic geometry
221 222 2636 Rascafría POLYGON ((436250.375 4539098.529, 436255.925 4...
df[df.pob_pad19==df.pob_pad19.max()]
codigo_geo pob_pad19 zona_basic geometry
158 159 63789 Mar Báltico POLYGON ((447970.223 4484559.958, 447972.423 4...

There are 286 basic health zones, with an average population of around 23 000 people: the Rascafría area has only 2 636 people; the Mar Báltico area has 63 789 people.

df["area"] = df['geometry'].area
df['pob_densidad'] = 100000*df.pob_pad19/df.area
df.head()
codigo_geo pob_pad19 zona_basic geometry area pob_densidad
0 001 30748 Abrantes POLYGON ((439068.758 4470731.661, 439076.433 4... 1.572304e+06 1955.600930
1 002 19432 Acacias POLYGON ((439924.930 4472798.281, 439928.742 4... 7.741107e+05 2510.235189
2 003 29168 Adelfas POLYGON ((443455.718 4472836.724, 443412.985 4... 8.528277e+05 3420.151457
3 004 21274 Alameda POLYGON ((440659.754 4473778.401, 440665.227 4... 5.452036e+05 3902.028813
4 005 29001 Alameda de Osuna POLYGON ((452408.138 4484644.700, 452415.138 4... 3.513729e+07 82.536255
fig, (ax1, ax2) = plt.subplots(ncols=2, sharex=True, sharey=True, figsize=(12,6))

df.plot(ax=ax1, column=df.pob_pad19, cmap='Reds', legend=True)
df.plot(ax=ax1, color='white', edgecolor='grey', alpha=0.1)
ax1.set_title('Total population\n by basic health area')
ax1.axis('off')

df.plot(ax=ax2, column=df.pob_densidad, cmap='Reds', legend=True)
df.plot(ax=ax2, color='white', edgecolor='grey', alpha=0.1)
ax2.set_title('Population density\n by basic health area')
ax2.axis('off');
df[df.pob_densidad==df.pob_densidad.min()]
codigo_geo pob_pad19 zona_basic geometry area pob_densidad
221 222 2636 Rascafría POLYGON ((436250.375 4539098.529, 436255.925 4... 2.581206e+08 1.021228
df[df.pob_densidad==df.pob_densidad.max()]
codigo_geo pob_pad19 zona_basic geometry area pob_densidad
160 161 17331 Martín de Vargas POLYGON ((440449.700 4472841.620, 440459.695 4... 307034.469165 5644.643107

Mapas de Zonas Básicas de Salud del Área Única de la Comunidad de Madrid

https://www.madrid.org/iestadis/fijas/estructu/general/territorio/estructucartemzbs.htm

(maps have been downloaded and stored in ./maps/...)

https://www.madrid.org/iestadis/fijas/estructu/general/territorio/descarga/zbs13_mar_baltico.pdf

#collapse-hide
# Convert downloaded map from .pdf to .png 

#convert_from_path('./maps/zbs13_mar_baltico.pdf')[0].save('./maps/159_mar_baltico.png', dpi=(300,300))
#convert_from_path('./maps/zbs13_rascafria.pdf')[0].save('./maps/222_rascafria.png', dpi=(300,300))
#convert_from_path('./maps/zbs13_martin_de_vargas.pdf')[0].save('./maps/161_martin_de_vargas.png', dpi=(300,300))

Image.open('./maps/222_rascafria.png')